import 'package:cached_network_image/cached_network_image.dart';
import 'package:fate_blind_box/widget/icon_text.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
import '../../im/chatPage.dart';
import '../../im/groupchat.dart';
import '../../network/Api.dart';
import '../../theme/theme_color.dart';
import '../../utils/provider.dart';

class MessageDataa extends StatefulWidget {
  EMConversation emMessage;

  MessageDataa(this.emMessage, {Key? key}) : super(key: key);

  @override
  State<MessageDataa> createState() => _MessageDataaState();
}

class _MessageDataaState extends State<MessageDataa> {
  final _regExp = r"^[a-z0-9_]+$";
  Map _userInfo = {};
  late EMMessage emmessage;
  EMTextMessageBody? connt;

  String membersex = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getMessagesInfo();
    _getMemberInfo();
    //_addChatListener();
  }
  _getMessagesInfo()async{
    EMMessage? emMessage = await widget.emMessage.latestMessage();

    connt = emMessage?.body as EMTextMessageBody;
    if (kDebugMode) {
     // print('-------啊阿萨斯---------'+connt.content);
    }
    setState(() {});
  }




  _getMemberInfo() async {
    final res = await Api.getInstance().getSessions(widget.emMessage.id);
    if (res['code'] == 0) {
      final data = res['data'];
      if (data != null) {
        setState(() {
          _userInfo = data;

          // if(_userInfo['index']==0){
          //   membersex='保密';
          // }
          // if(_userInfo['index']==1){
          //   membersex='男';
          // }
          // if(_userInfo['index']==2){
          //   membersex='女';
          // }
          //
          // if(_userInfo['index']>=3){
          //   membersex=_userInfo['index'].toString();
          // }
          if (kDebugMode) {
            print('--------------' + _userInfo.toString());
          }
        });
      } else {
        // ThemeSnackBar.show(context, "用户信息错误");
      }
    } else {
      // ThemeSnackBar.show(context, res['msg']);
    }
  }

  @override
  Widget build(BuildContext context) {
    return connt == null ? Container() : InkWell(
      onTap: () {
        if (widget.emMessage.id.isNotEmpty&&_userInfo['owner']!=null) {
          if (RegExp(_regExp).firstMatch(widget.emMessage.id) != null) {
            Navigator.of(context).push(MaterialPageRoute(
                builder: (builder) => GroupChat(widget.emMessage.id,_userInfo['owner'])));
          }else{

            Navigator.of(context).push(MaterialPageRoute(
                builder: (builder) => ChatPage(widget.emMessage.id)));

          }
        }
      },
      child: _userInfo.isNotEmpty?Container(
        margin: const EdgeInsets.only(top: 10),
        padding: const EdgeInsets.only(
          top: 10.0,
          bottom: 10.0,
        ),
        decoration: const BoxDecoration(
          border:
              Border(bottom: BorderSide(color: Color(0xffEDEDED), width: 10)),
        ),
        child: Container(
          margin: const EdgeInsets.only(left: 16, right: 16),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                  margin: const EdgeInsets.all(5),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(5),
                    child: Image(
                        image: CachedNetworkImageProvider(
                            Provider.fixAvatar(_userInfo['photoUrl'])),
                        height: 45,
                        width: 45,
                        fit: BoxFit.fill),
                  )),
              Flexible(

                child: Container(
                  // padding: const EdgeInsets.only(top: 6,bottom: 5),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              alignment: Alignment.center,
                              decoration: ShapeDecoration(
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(8),
                                  ),
                                  color: _userInfo['index'] == 2 ? ThemeColor.mainColor : Colors.blue
                              ),
                              width: 36,
                              height: 18,
                              child: _userInfo['index']>=3?Container(
                                child: Text(_userInfo['index'].toString(), style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 12,
                                ),),
                              ) : IconText(
                                _userInfo['index'] == 2 ? "女" : "男",
                                icon: Icon(
                                    _userInfo['index'] == 2 ? Icons.female : Icons.male,
                                    color: Colors.white
                                ),
                                iconSize: 12,
                                style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 12,
                                ),
                              ),
                            ),
                            Container(
                              width: 180,
                              margin: const EdgeInsets.only(left: 5),
                              child: Text(
                                _userInfo['nickName'] ?? "",
                                style: const TextStyle(
                                  color: Colors.black,
                                  fontSize: 14,
                                ),
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                            const Text(
                              "21.30",
                              style:
                                  TextStyle(fontSize: 14, color: Colors.grey),
                            ),
                          ],
                        ),
                      ),
                      Container(
                          child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            margin: const EdgeInsets.only(top: 5,left: 8),
                            child:  Text(connt?.content??"",
                              style: const TextStyle(
                                  color: Colors.grey, fontSize: 14),
                            ),
                          ),
                          Container(
                            padding: const EdgeInsets.only(
                                left: 5, right: 5, top: 2, bottom: 2),
                            decoration: BoxDecoration(
                              color: Colors.red,
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(20.0)),
                              border: Border.all(width: 1, color: Colors.white),
                            ),
                            child: const Text(
                              '1',
                              style:
                                  TextStyle(color: Colors.white, fontSize: 10),
                            ),
                          ),
                        ],
                      ))
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ):Container(),
    );
  }
}
